package com.lin.game.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.game.entity.User;
import com.lin.game.dao.UserDao;
import com.lin.game.utils.MapCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

@Service
@Primary
public class UserDBServiceImpl implements IUserService{
    @Autowired
    private UserDao userDao;

    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public int register(User user){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",user.getUsername());
        int count=userDao.selectCount(queryWrapper);
        if(count>0){
            return -1;
        }
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("email",user.getEmail());
        int ecount=userDao.selectCount(queryWrapper1);
        if(ecount>0){
            return -2;
        }
        return userDao.insert(user);
    }

    @Override
    public User login(String username, String password) {
        System.out.println(username+password);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        queryWrapper.eq("password",password);
        User user=userDao.selectOne(queryWrapper);
        System.out.println(user);
        return user;
    }
    @Override
    public int sendEmailByUserName(String username) {

        //判断用户名是否存在
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", username);
        int count = userDao.selectCount(queryWrapper);
        if (count == 0) {
            //用户名不存在
            return -1;
        }

        //通过用户名查询邮箱
        QueryWrapper queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("username", username);
        User user = userDao.selectOne(queryWrapper2);
        //获取当前用户的邮箱
        String email = user.getEmail();

        //生成一个验证码 4   [1000 ~ 10000) 1000 ~ 9999
        int code = (int) (Math.random() * 9000 + 1000);
        //保存验证码
        MapCodeUtils.putCode(username,code+"");
        //给当前邮箱发送邮件
        //mimeMessage邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper=new MimeMessageHelper(mimeMessage);
            mimeMessageHelper.setSubject("找回密码邮件");
            mimeMessageHelper.setFrom("lh935072326@sina.com");
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setText("<h1>找回密码</h1>找回的验证码："+code+",如果非本人操作请忽略",true);
            mimeMessageHelper.setSentDate(new Date());
           // mimeMessageHelper.addAttachment("我的附件",new File("C"));
            //发送邮件
            javaMailSender.send(mimeMessage);
            return  1;
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return -2;
    }

    @Override
    public int updatePassword(String username, String newPassword) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        User user=userDao.selectOne(queryWrapper);
        user.setPassword(newPassword);
        int reslut=userDao.updateById(user);
        return reslut;
    }

    @Override
    public int updateHeader(String username, String header) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("username",username);
        User user=userDao.selectOne(queryWrapper);
        user.setHeader(header);

        int reslut=userDao.updateById(user);
        return reslut;
    }

}
